from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.urls import reverse
from django.db.models import Q
# Create your views here.
from django.utils.html import format_html
from ..models import Types, JsonExtendEncoder

import datetime
import time
import json


# 栏目分类
def GetTree(data, pid):
    tree = []

    for k in data:
        if k['t_pid'] == pid:
            return tree.append(k)
        else:
            return GetTree(data, k['t_pid'])
    return tree

# 查询条件


def getKwargs(data={}):
    kwargs = {}
    for (k, v) in data.items():
        if v is not None and v != u'':
            kwargs[k] = v
    try:
        if 't_name' in kwargs:
            udate = kwargs['t_name']
            kwargs.pop('t_name')
            object = Types.objects.filter(
                Q(t_name__contains=udate)).filter(**kwargs.values())
        else:
            object = Types.objects.filter(**kwargs).values()
    except:
        object = Types.objects.all().values()

    count = object.count()
    for k in object:

        if k['t_pid'] == 0:
            k['pid_name'] = '顶级栏目'
        else:
            k['pid_name'] = Types.objects.get(id=k['t_pid']).t_name

    #ab = GetTree(object,0)
    # print(ab)
    data = json.dumps(list(object), cls=JsonExtendEncoder)
    data = {'data': data, 'count': count}
    return data


# 产品类别列表
def typelist(request):
    data = getKwargs(request.GET)

    return render(request, 'back/types/list.html', data)

# 产品类别添加


def typeadd(request):
    if request.method == 'GET':
        type = Types.objects.all()
        return render(request, 'back/types/add.html', {'type': type})

    elif request.method == 'POST':
        try:
            object = Types()
            object.t_name = request.POST['t_name']
            object.t_pid = request.POST['t_pid']
            object.t_info = request.POST['t_info']
            object.t_isdel = 0
            if request.POST['t_pid'] == '0':
                object.t_path = '0,'
            else:
                obj = Types.objects.get(id=request.POST['t_pid'])
                object.t_path = '{p}{c},'.format(
                    p=obj.t_path, c=request.POST['t_pid'])
            object.save()
            return JsonResponse({'code': 1, 'msg': '添加栏目成功'})
        except:
            return JsonResponse({'code': 0, 'msg': '添加栏目失败', 'href': reverse('typelist')})

        # return HttpResponse(1)

# 产品类别详情


def typedetail(request):
    object = Types.objects.filter(id=request.GET.get('id')).values()
    for k in object:
        if k['t_pid'] == 0:
            k['pid_name'] = '顶级栏目'
        else:
            k['pid_name'] = Types.objects.filter(id=k['t_pid'])[0].t_name

    return render(request, 'back/types/detail.html', {'data': object[0]})

# 产品类别修改


def typeedit(request):

    if request.method == 'GET':
        object = Types.objects.filter(id=request.GET.get('id')).values()
        type = Types.objects.all()
        return render(request, 'back/types/edit.html', {'data': object[0], 'type': type})

    elif request.method == 'POST':
        try:
            object = Types.objects.get(id=request.POST['id'])
            object.t_name = request.POST['t_name']
            object.t_pid = request.POST['t_pid']
            object.t_info = request.POST['t_info']

            if request.POST['t_pid'] == '0':
                object.t_path = '0,'
            else:
                obj = Types.objects.get(id=int(request.POST['t_pid']))
                object.t_path = '{p}{c},'.format(
                    p=obj.t_path, c=request.POST['t_pid'])

            object.save()
            return JsonResponse({'code': 1, 'msg': '修改信息成功'})
        except:
            return JsonResponse({'code': 0, 'msg': '修改信息失败', 'href': reverse('typelist')})

        # return HttpResponse(0)


# 审核类别状态
def type_change_status(request):
    status = request.POST.get('status')
    object = Types.objects.get(id=request.POST.get('id'))
    try:

        if status == '0':
            object.t_isdel = 1
        else:
            object.t_isdel = 0
        object.save()
        data = getKwargs(request.POST)
        return JsonResponse({'code': 1, 'pid': object.t_isdel, 'msg': '修改成功', 'data': data})
    except:
        return JsonResponse({'code': 0, 'pid': object.t_isdel, 'msg': '修改失败'})


# 产品类别显示状态
def typedel(request):
    try:
        object = Types.objects.get(id=request.GET.get('id'))
        object.t_isdel = 0
        object.save()
        return JsonResponse({'code': 1, 'msg': '栏目删除成功'})
    except:
        return JsonResponse({'code': 0, 'msg': '栏目删除失败', 'href': reverse('typelist')})


#
# #会员列表
# def userlist(request):
#     search = request.GET
#     kwargs = getKwargs(search)
#
#     try:
#         if 'u_addtime' in kwargs:
#             udate = kwargs['u_addtime']
#             kwargs.pop('u_addtime')
#             object = User.objects.filter(Q(u_addtime__contains=udate)).filter(**kwargs).values()
#         else:
#             object = User.objects.filter(**kwargs).values()
#         count = object.count()
#     except User.DoesNotExist:
#         object = User.objects.all().values()
#         count = object.count()
#
#
#     for k in object:
#         k['level_name'] = User.objects.get(id=k['id']).u_level.l_name
#     data = json.dumps(list(object), cls=JsonExtendEncoder)
#
#     return render(request,'back/user/list.html',{'data':data,'count':count})
#
# #会员查看
# def detail(request):
#
#     id= request.GET.get('id')
#     data = User.objects.get(id=id)
#
#     return render(request,'back/user/detail.html',{'data':data})
#
# #审核会员
# def changestatus(request):
#
#     id = request.GET.get('id')
#     stacode = request.GET.get('status')
#     if stacode == 'true':
#         stacode = '已审核'
#     else:
#         stacode = '未审核'
#
#     try:
#         object=User.objects.get(id=id)
#         object.u_status = stacode
#         object.save()
#         stacode = object.u_status
#         return JsonResponse({'code':1,'stacode':stacode,'msg':'审核成功'})
#     except:
#         return JsonResponse({'code': 0, 'stacode': stacode,'msg':'审核失败'})
